export default class {
  prese(num1 = 0, num2 = 0) {
    let r1, r2
		try {
			r1 = num1.toString().split('.')[1].length
		} catch (e) {
			r1 = 0
		}
		try {
			r2 = num2.toString().split('.')[1].length
		} catch (e) {
			r2 = 0
		}
    return { r1, r2 }
  }
	add(num1 = 0, num2 = 0) {
    const { r1, r2 } = this.prese(num1, num2)
		const m = Math.pow(10, Math.max(r1, r2))
		return (this.mul(num1, m) + this.mul(num2, m)) / m
	}
	// 减法
	sub(num1 = 0, num2 = 0) {
		const { r1, r2 } = this.prese(num1, num2)
		const m = Math.pow(10, Math.max(r1, r2))
		return (this.mul(num1, m) - this.mul(num2, m)) / m
	}
	// 乘法
	mul(num1 = 0, num2 = 0) {
    const { r1, r2 } = this.prese(num1, num2)
    const m = r1 + r2
    const n1 = Number(num1.toString().replace('.', ''))
    const n2 = Number(num2.toString().replace('.', ''))
		return (n1 * n2) / Math.pow(10, m)
	}
	// 除法
	div(num1 = 0, num2 = 0) {
    const { r1, r2 } = this.prese(num1, num2)
    const n1 = Number(num1.toString().replace('.', ''))
    const n2 = Number(num2.toString().replace('.', ''))
		return (n1 / n2) * Math.pow(10, r2 - r1)
	}
}

// https://github.com/xiaolidan00/my-earth